using System;

namespace OpenWaves.UnitTests.EPiServer.CustomProperties
{
    public static class PropertyValueScope
    {
        public static PropertyValueScope<T> Create<T>(Func<T> getValue, Action<T> setValue, T value)
        {
            return new PropertyValueScope<T>(getValue, setValue, value);
        }
    }
    public class PropertyValueScope<T> : IDisposable
    {
        private readonly Action<T> setValue;

        private readonly T previousValue;

        public PropertyValueScope(Func<T> getValue, Action<T> setValue, T value)
        {
            this.setValue = setValue;

            this.previousValue = getValue();

            setValue(value);
        }

        public void Dispose()
        {
            setValue(previousValue);
        }
    }
}